<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
use app\admin\model\Equipgroup as thisModel;
use app\admin\model\Equipment as EquipmentModel;
class Equipgroup extends Common
{
    function index(){
        $thisModel = new thisModel();
        $list = $thisModel->mainAllList();
        // dump($list);exit;

        $this->assign('list',$list);
        return $this->fetch('index');
    }

    function mainEdit(){
        $thisModel = new thisModel();
        if (request()->isAjax()) {
            $postData = input('post.');
            $id = input('post.id');
            $data = $thisModel->disposeData(input('post.'));
           // dump($data);exit;
            if($id > 0){
                $res = $thisModel->save($data,['id'=>$id]);
                if($res !== false){
                    exitjson(1, '保存成功');
                }else{
                    exitjson(0, '保存失败');
                }
            }else{
                $res = $thisModel->save($data);
                if ($res) {
                    exitjson(1, '保存成功');
                } else {
                    exitjson(0, '保存失败');
                }
            }
        } else {

            if(input('id') > 0){
                $one = $thisModel->get(input('id'));
                $this->assign('one',$one);
            }
           // dump($one);exit;
            return $this->fetch('mainEdit');
        }
    }

    function form(){
        $thisModel = new thisModel();
        $data = $thisModel->disposeData(input('post.'));
        $id = input('post.id');
        if(empty($id)){
            $res = $thisModel->save($data);
            if($res){
                $this->success('添加成功','Equipgroup/index','',0);
            }else{
                $this->error('添加失败','Equipgroup/index','',0);
            }
        }else{
            $res = $thisModel->save($data,['id'=>$id]);
            if($res !== false){
                $this->success('编辑成功','Equipgroup/index','',0);
            }else{
                $this->error('编辑失败','Equipgroup/index','',0);
            }
        }
    }

    function mainDel(){
        $id = input('id');
        if($id>0){
            $thisModel = new thisModel();
            $one = $thisModel->get($id);
            $res = $one->delete();
            if($res !== false){
                exitjson(1,'删除成功');
            }else{
                exitjson(0,'删除失败');
            }
        }
    }

    function equipgroup(){
        $thisModel = new thisModel();
        $EquipmentModel = new EquipmentModel();
        $id = input('route.id');
        $group_data = $thisModel->get($id);

        //设备
        $equipment_list = $EquipmentModel->mainAllList();

        $group_data['rules'] = explode(',',$group_data['rules']);
        // $rule_data = $thisModel->getTreeData('level','id','equipment');
        // dump($group_data);
        // dump($rule_data);exit;
        $assign = array(
            'group_data'=>$group_data,
            'rule_data'=>$rule_data,
            'equipment_list' => $equipment_list
        );
        $this->assign($assign);
        return view('equipgroup');
    }

    function groupForm(){
        $data = input('post.');
        $data['rules'] = implode(',',$data['rule_ids']);
        $id = $data['id'];
        unset($data['id']);
        unset($data['rule_ids']);
        if(empty($id)){
            $res = db('equip_group')->insertGetId($data);
            if($res){
                $json = array('status' => 1,'msg'=>'添加成功','data'=>'Equipgroup/index');
            }else{
                $json = array('status'=>2,'msg'=>'添加失败','data'=>'Equipgroup/index');
            }
            echo json_encode($json);exit;
        }else{
            $res = db('equip_group')->where(array('id'=>$id))->update($data);
            if($res !== false){
                $json = array('status'=>1,'msg'=>'操作成功','data'=>'Equipgroup/index');
            }else{
                $json = array('status'=>2,'msg'=>'操作失败','data'=>'Equipgroup/index');
            }
            echo json_encode($json);exit;
        }
    }


}